package com.nwpu.coursesupport.view.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nwpu.coursesupport.controller.AbstractController;
import com.nwpu.coursesupport.entity.bean.JSONResult;
import com.nwpu.coursesupport.enumeration.RoleType;
import com.nwpu.coursesupport.service.imple.TaskGradeListViewService;
import com.nwpu.coursesupport.tool.JSONUtils;
import com.nwpu.coursesupport.view.criteria.TaskGradeListViewSearchCriteria;

@Controller
@RequestMapping("/taskGradeListView")
public class TaskGradeListViewController extends AbstractController{

	@Autowired
	private TaskGradeListViewService  taskGradeListViewService;
	
	@RequestMapping("/ajaxTListData")
	@ResponseBody
	public JSONResult ajaxTListData(@RequestParam("data")String data){
		JSONResult rst=invokeService("taskGradeListView", "listView" ,data);
		return rst;
	}
	
	@RequestMapping("/ajaxSListData")
	@ResponseBody
	public JSONResult ajaxSListData(@RequestParam("data")String data){
		JSONResult rst=new JSONResult();
		rst.fail();
		TaskGradeListViewSearchCriteria sc=JSONUtils.json2pojo(data, TaskGradeListViewSearchCriteria.class);
		if(sc==null){
			return rst.fail("参数有误");
		}
		try{
			int id =(int) getSession().getAttribute("id");
			sc.setUid(id);
			sc.setRoleType(RoleType.STUDENT);
			rst=this.taskGradeListViewService.listView(sc);
		}catch(Exception e){
			return rst.fail("后台出错");
		}
		return rst.success();
	}
	
	@RequestMapping("/slistData")
	public String slistData(@RequestParam("courseId")int courseId,Integer pageNo,HttpServletRequest request){
		JSONResult rst=new JSONResult();
		rst.fail();
		TaskGradeListViewSearchCriteria sc=new TaskGradeListViewSearchCriteria();
		sc.setCourseId(courseId);
		sc.setPageNo(pageNo==null?1:pageNo);
		int id =(int) getSession().getAttribute("id");
		sc.setUid(id);
		sc.setRoleType(RoleType.STUDENT);
		try{
			rst=this.taskGradeListViewService.listView(sc);
			request.setAttribute("data", this.parseData(rst));
		}catch(Exception e){
			rst.fail("后台出错");
			request.setAttribute("data", this.parseData(rst));
		}
		return "";
	}
	
}
